package com.jirefox.loner.runtime.service;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.DisposableBean;

import groovy.sql.Sql;

public class SqlHolder implements DisposableBean {

	private Map<String, DataSource> dataSourceStore = new HashMap<String, DataSource>();

	public Sql getSql(String name) {
		DataSource dataSource = dataSourceStore.get(name);
		Sql sql = new Sql(dataSource);
		return sql;
	}

	public void addDataSource(String name, DataSource dataSource) {
		dataSourceStore.put(name, dataSource);
	}

	@Override
	public void destroy() throws Exception {
		for (Map.Entry<String, DataSource> entry : dataSourceStore.entrySet()) {
			DataSource dataSource = entry.getValue();
			org.apache.tomcat.jdbc.pool.DataSource jdbcDataSource = (org.apache.tomcat.jdbc.pool.DataSource) dataSource;
			jdbcDataSource.close(true);
		}

	}

}
